home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1993…ch: Other People's Memory / ADC Developer CD (1993-03) (''Other People's Memory'')_iso / Dev.CD Mar 93.iso / Technical Documentation / Sample Code / DTS.Lib & Samples / Kibitz / GoToMove.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-22  |  4.4 KB  |  191 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        gotomove.c
  5. ** Written by:  Eric Soldan
  6. **
  7. ** Copyright © 1990-1992 Apple Computer, Inc.
  8. ** All rights reserved. */
  9.  
  10.  
  11.  
  12. /*****************************************************************************/
  13.  
  14.  
  15.  
  16. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  17. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  18. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  19.  
  20. #ifndef __PACKAGES__
  21. #include <Packages.h>
  22. #endif
  23.  
  24. #ifndef __RESOURCES__
  25. #include <Resources.h>
  26. #endif
  27.  
  28. #ifndef __TOOLUTILS__
  29. #include <ToolUtils.h>
  30. #endif
  31.  
  32. #ifndef __UTILITIES__
  33. #include <Utilities.h>
  34. #endif
  35.  
  36.  
  37.  
  38. /*****************************************************************************/
  39.  
  40.  
  41.  
  42. #define kGoToOK        1
  43. #define kGoToEquivs    2
  44. #define kGoToCancel    3
  45. #define kStatText1    4
  46. #define kWhichMove    5
  47. #define kWhiteMove    6
  48. #define kBlackMove    7
  49.  
  50. #define kLeftArrow    28
  51. #define kRightArrow    29
  52. #define kUpArrow    30
  53. #define kDownArrow    31
  54.  
  55. static short    gOption;
  56.  
  57. static pascal Boolean    GoToFilter(DialogPtr dlg, EventRecord *event, short *item);
  58.  
  59.  
  60.  
  61. /*****************************************************************************/
  62. /*****************************************************************************/
  63.  
  64.  
  65.  
  66. #pragma segment GoTo
  67. void    DoGoToMove(FileRecHndl frHndl)
  68. {
  69.     WindowPtr        oldPort, window;
  70.     DialogPtr        dialog;
  71.     short            item, userItemType, startColor, halfMoveNum, moveNum;
  72.     short            gameIndex, whosMove, i;
  73.     Handle            itemHndl;
  74.     Rect            userItemRect;
  75.     TEHandle        te;
  76.     Str255            str;
  77.     TheDocPtr        docPtr;
  78.  
  79.     oldPort = SetFilePort(frHndl);
  80.  
  81.     window = (*frHndl)->fileState.window;
  82.     if (dialog = GetCenteredDialog(rGoToMove, nil, window, (WindowPtr)-1L)) {
  83.  
  84.         startColor  = (*frHndl)->doc.startColor;
  85.         halfMoveNum = (*frHndl)->doc.gameIndex + startColor;
  86.         moveNum     = (halfMoveNum >> 1);
  87.  
  88.         item = kWhiteMove + (halfMoveNum - 2 * moveNum);
  89.         GetDItem(dialog, item, &userItemType, &itemHndl, &userItemRect);
  90.         SetCtlValue((ControlHandle)itemHndl, true);
  91.  
  92.         te = ((DialogPeek)dialog)->textH;
  93.         pcpydec(str, moveNum + 1);
  94.         TESetText(str + 1, *str, te);
  95.         TESetSelect(0, *str, te);
  96.  
  97.         for (;;) {
  98.  
  99.             SetPort(dialog);
  100.             ModalDialog((ModalFilterProcPtr)GoToFilter, &item);
  101.  
  102.             if (item == kGoToOK) {
  103.                 GetDItem(dialog, kBlackMove, &userItemType, &itemHndl, &userItemRect);
  104.                 whosMove = GetCtlValue((ControlHandle)itemHndl);
  105.                 BlockMove(*((*te)->hText), str + 1, *str = (*te)->teLength);
  106.                 break;
  107.             }
  108.  
  109.             if (item == kGoToCancel) break;
  110.  
  111.             if (item >= kWhiteMove) {
  112.                 GetDItem(dialog, item, &userItemType, &itemHndl, &userItemRect);
  113.                 SetCtlValue((ControlHandle)itemHndl, true);
  114.                 item = (kWhiteMove + kBlackMove) - item;
  115.                 GetDItem(dialog, item, &userItemType, &itemHndl, &userItemRect);
  116.                 SetCtlValue((ControlHandle)itemHndl, false);
  117.             }
  118.         }
  119.  
  120.         DisposeDialog(dialog);
  121.  
  122.         if (item == kGoToOK) {
  123.             moveNum   = p2num(str, 10, nil) - 1;
  124.             gameIndex = ( 2 * moveNum) + whosMove - startColor;
  125.             if (gOption) {
  126.                 moveNum = gameIndex = 0;
  127.                 docPtr  = &((*frHndl)->doc);
  128.                 if (docPtr->timeLeft[0] != -1)
  129.                     for (i = 0; i < 2; ++i)
  130.                         docPtr->displayTime[i] = docPtr->timeLeft[i] = docPtr->defaultTime[i];
  131.             }
  132.             if ((moveNum >= 0) && (gameIndex >= 0)) {
  133.                 SetFilePort(frHndl);
  134.                 RepositionBoard(frHndl, gameIndex, true);
  135.                 ImageDocument(frHndl, true);
  136.                 AdjustGameSlider(frHndl);
  137.                 if ((*frHndl)->doc.twoPlayer) SendGame(frHndl, kResync, nil);
  138.                 SendMssg(frHndl, kTimeMssg);
  139.                 (*frHndl)->doc.timerRefTick = TickCount();
  140.                 DrawTime(frHndl);
  141.             }
  142.         }
  143.     }
  144.  
  145.     SetPort(oldPort);
  146. }
  147.  
  148.  
  149.  
  150. /*****************************************************************************/
  151.  
  152.  
  153.  
  154. #pragma segment Config
  155. pascal Boolean    GoToFilter(DialogPtr dlg, EventRecord *event, short *item)
  156. {
  157.     TEHandle                te;
  158.     char                    key;
  159.     Boolean                    arrowKey;
  160.     static unsigned long    lastIdle;
  161.  
  162.     gOption = (event->modifiers) & optionKey;
  163.  
  164.     if (KeyEquivFilter(dlg, event, item)) return(true);
  165.  
  166.     *item = 0;
  167.  
  168.     switch (event->what) {
  169.         case keyDown:
  170.         case autoKey:
  171.             te       = ((DialogPeek)dlg)->textH;
  172.             key      = event->message & charCodeMask;
  173.             arrowKey = ((key >= kLeftArrow) && (key <= kDownArrow));
  174.             if ((arrowKey) || (key == 8)) return(false);    /* These are always okay. */
  175.             if ((key < '0') || (key > '9')) return(true);    /* Don't accept non-digits. */
  176.             if ((*te)->teLength >= 3) return(true);            /* Don't accept more than 3 chars. */
  177.             break;
  178.     }
  179.  
  180.     if (lastIdle + 30 < TickCount()) {
  181.         lastIdle = TickCount();
  182.         DoIdleTasks(false);
  183.         SetPort(dlg);
  184.     }
  185.  
  186.     return(false);
  187. }
  188.  
  189.  
  190.  
  191.